﻿function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function() {
			if (oldonload) {
				oldonload();
			}
			func();
		}
	}
}

function noBubbles(e){
	if (typeof e!="undefined")
		e.stopPropagation()
	else
		event.cancelBubble=true
}

function traversSubTree(node) {
	var childs = node.getElementsByTagName("li");
	for(var i = 0; i < childs.length; i++) {
		var uls = childs[i].getElementsByTagName("ul");
		if (uls.length > 0) {
			childs[i].className = "submenu";
			/*if(uls[0].style.display == "" || typeof uls[0].style == "undefined") {
				uls[0].style.display = "block";
			} else if(uls[0].style.display == "none") {
				childs[i].style.listStyleImage = "url('./Images/closed.png')";
			} */
			if(uls[0].style.display == "" || typeof uls[0].style == "undefined" || uls[0].style.display == "none") {
				uls[0].style.display = "none";
				childs[i].style.listStyleImage = "url('./Images/closed.png')";
			}
			childs[i].onclick = function(e) {
				var ul = this.getElementsByTagName("ul")[0];
				if (typeof ul != "undefined") {
					 if (ul.style.display != "block") {
							ul.style.display = "block";
							this.style.listStyleImage = "url('./Images/opened.png')";
						} else {
							ul.style.display = "none";
							this.style.listStyleImage = "url('./Images/closed.png')";
						}
					ul.onclick = function(e) {
						noBubbles(e);
					}
				}
				noBubbles(e);
			}
		} else {
			childs[i].onclick = function(e) {
				noBubbles(e);
			}
		}
	}
	node.onclick = function(e) {
		noBubbles(e);
	}
}

function setOnClickEvents() {
	var rootNode = document.getElementById("menu");
	traversSubTree(rootNode);
}

//--------AJAX functions------//

function setContent(url) {
	if (url != "")
	new Ajax.Request(url, { method: 'post',parameters: {id: 1, loc: 2},
		onSuccess: function(transport){
		$('content').innerHTML = transport.responseText;
		}
	});
}

addLoadEvent(setOnClickEvents);